#include<stdio.h>
 struct node
{
   int data;
   struct node *next;
}*p;

  


//function to add node(append function)

void append()
   {  int num;
         printf("enter a number :-");
         scanf("%d",&num);
     struct node *temp,*r;
     

       temp= (struct node *)malloc(sizeof(struct node));
       temp->data=num;
       r=(struct node *)p;

      if (p == NULL) 
     {
         p=temp;
         p->next =NULL;
     }
  else
     {        

       while( r->next != NULL)
       r=r->next;
       r->next =temp;
       r=temp;
       r->next=NULL;
     }
     display(p);
}

 // function to add a node at a specified location,index function

 void index()
  {   int num, index,k;
      printf("enter a number:-");
      scanf("%d",&num);
      printf("enter the index where you want to insert (index of the list is starting from 1):-");
      scanf("%d",&index);
     int i;
     struct node *temp,*t,*r;
     r=p;       /* here r stores the first location */
      if(index > count()+1 || index <= 0)
   {
         printf("insertion is not possible ");
         printf("\n");
            return;
   }
        if (index == 1)/* if list is null then add at beginning */
         {
           addbeg(num);
           return;
         }
      else
 {
       for(i=1;i<index;i++)
          {
             t=r;   /* t will be holding previous value */
             r=r->next;
          }
         temp=(struct node *)malloc(sizeof(struct node));
         temp->data=num;
         t->next=temp;
         t=temp;
         t->next=r;
        return;
       }
  }
       
      //function to add a node at the beginning 
       addbeg( int num )
   {
        

   struct node *temp;
        temp=(struct node *)malloc(sizeof(struct node));
        temp->data=num;

       /* IF LIST IS NULL ADD AT BEGINNING  */
        if ( p== NULL)
       {
          p=temp;
          p->next=NULL;
       }

   else
      {
          temp->next=p;
          p=temp;
      }
   }
       
 //function to delete a node

void delete()
{
   int    num;
      printf("enter a number to delete :");
      scanf("%d",&num);
struct node *temp, *m;
temp=p;
      while(temp!=NULL)
    {
       if(temp->data==num)
       {
           if(temp==p)
           {
              p=temp->next;
              free(temp);
              return;
           }
           else
         {
           m->next=temp->next;
           free(temp);
           return;
         }
      }
      else
        {
           m=temp;
          temp= temp->next;
        }
        

}
    printf("element you entered : %d is not in the list\n ", num);
}

//function to count the number of elements
count()
 {
      {
   struct node *n;
   int c=0;
   n=p;
    while(n!=NULL)
   {
     n=n->next;
     c++;
   }
  return(c);
  
 }
 printf("\n");
 }
 
 
 
//function to reverse the linked list
reverse(struct node *q)
{
   struct node *m, *n,*l,*s;
   m=q;
   n=NULL;
  while(m!=NULL)
 {
   s=n;
   n=m;
  m=m->next;
  n->next=s;
 }
   p=n;
}
//function to display the contents 

  display(struct node *r)
  {
       {
      r=p;
      if(r==NULL)
     {
       printf("list is empty :\n");
       return;
     }
       /* traverse the entire linked list */
       while(r!=NULL)
    {
      printf("  %d ,  ",r->data);
      r=r->next;
    }
     printf("");
       }
       printf("\n");

  }
  
sort(struct node *p)
{	
	struct node *t, *s, *r;
	t=(struct node *)malloc(sizeof(struct node));
	s=(struct node *)malloc(sizeof(struct node));
	r=(struct node *)malloc(sizeof(struct node));
	int temp;
	t=p;
	r=p;
      if(r==NULL)
     {
       printf("list is empty :\n");
       return;
     }
 while(r!=NULL)
    {
      r=r->next;
    }
	for(;p!=NULL;p=p->next)
	{
		for(s=p->next;s!=r;s=s->next)
		{
			if((s->data)<(p->data))
			{
				temp=s->data;
				s->data=p->data;
				p->data=temp;
			}
		}
	}
	display(t);
}





//MAIN PROGRAM

  main()
 {
        int i;
   p=NULL;
   while(1) /* this is an indefinite loop */
 {
    printf("1.append function..\n");
    printf("2.insert function..\n");
    printf("3.remove function..\n");
    printf("4.count function..\n");
    printf("5.reverse function..\n");
    printf("6.sort function..\n");
    printf("7.to display the list..\n");
    printf("8.press 8 to exit the program..\n");
    printf("\n");
    printf("enter a number according to the function you want to perform( from above):\n");

    scanf("%d",&i); 

      switch(i)
    {
 //append function      
          case 1:
       {
         
         append();
         break;
       }

//inserts the number at the specified position
   case 2:
     {
      
      index();
      break;
    }  
    
  //deletes the specified number from the linked list  
   case 3:
     {
    
      delete();
     break;
    }
    
   
  
//counts the number of elements in the list
      case 4:
   {
      struct node *n;
      display(n);
      printf(" total number of elements in the list are %d \n",count());
      break;
   } 
      
//reverses the linked list
   case 5:
    {
      reverse(p);
        display(p);
        break;
    }
    
  //sort function 
   case 6 :sort(p);
	break;
   
 //to display the elements of the linked list   
    case 7:
      {
         struct node *n;
         printf("list list is : ");
         display(n);
         break;
      }
      
  //to exit the program
   case 8:
      {
         exit(0);
      }
 }
 }
}
